package com.company.bs2023;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author jianf
 * @date 2023年04月10 18:32
 */
public class KuaiDiXiaoGe {
    public static void main(String[] args) {
        KuaiDiXiaoGe xg = new KuaiDiXiaoGe();
        int n = 5;
        int x = 3;
        int y = 3;
        int[] a1 = {1, 2, 3, 4, 5};
        int[] b = {5, 4, 3, 2, 1};
        int[] a2 = {10, 10, 10, 10, 10};

        System.out.println(xg.calcMaxPay2(n, x, y, a2, b));
        // System.out.println(xg.calcMaxPay1(n, x, y, a2, b));
    }

    public int calcMaxPay2(int n, int x, int y, int[] a, int[] b) {
        return calcPay(0, 0, n, x, y, a, b);
    }

    private int calcPay(int i, int pay, int n, int x, int y, int[] a, int[] b) {
        if (i == n) {
            return pay;
        }
        int paya =0;// calcPay(i + 1, pay + a[i], n, x, y, a, b);
        int payb =0;// calcPay(i + 1, pay + b[i], n, x, y, a, b);

        if (x > 0) {
            x--;
            paya = calcPay(i + 1, pay + a[i], n, x, y, a, b);

        } else {
            paya = calcPay(i + 1, pay, n, x, y, a, b);
        }

        if (y > 0) {
            y--;
            payb = calcPay(i + 1, pay + b[i], n, x, y, a, b);
        } else {
            payb = calcPay(i + 1, pay, n, x, y, a, b);
        }
        if (paya > payb) {
            y++;
            payb -= b[i];
            return paya;
        } else {
            x++;
            paya -= a[i];
            return payb;
        }

    }

    public int calcMaxPay1(int n, int x, int y, int[] a, int[] b) {
        int result = 0;
        List<Item> ab = new ArrayList<>(n);
        List<Item> ba = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            ab.add(new Item(a[i] - b[i], i));
            ba.add(new Item(a[i] - b[i], i));
        }

        int m = Math.max(x, y);
        int[] ai = new int[m];
        int[] bi = new int[m];
        ab = ab.stream().sorted((x1, x2) -> (x2.getValue() - x1.getValue())).collect(Collectors.toList());
        ba = ba.stream().sorted((x1, x2) -> (x1.getValue() - x2.getValue())).collect(Collectors.toList());
        for (int j = 0; j < m; j++) {
            ai[j] = ab.get(j).getIndex();
            bi[j] = ba.get(j).getIndex();
            System.out.println(String.format("ai[j]=%d", ai[j]));
        }
        for (int i = 0; i < n; i++) {
            if (i < x) {
                result += a[ai[i]];
                System.out.println(String.format("a[]中选取，i=%d,值=%d", ai[i], a[ai[i]]));
            } else {
                result += b[bi[i - x]];
                System.out.println(String.format("b[]中选取，i=%d,值=%d", bi[i - x], b[bi[i - x]]));
            }
        }
        return result;
    }

    private class Item {
        private Integer value;
        private Integer index;

        public Item(int value, int index) {
            this.value = value;
            this.index = index;
        }

        public int getValue() {
            return value;
        }

        public int getIndex() {
            return index;
        }

    }
}